package com.tws.pctask.service;

import com.tws.common.model.ResponseWrapper;
import com.tws.common.tool.CommonTool;
import com.tws.common.tool.DataProcessing;
import com.tws.dao.entity.User;
import com.tws.pctask.mapper.DepartmentManageMapper;
import com.tws.pctask.model.departmentManageModel.*;
import com.tws.pctask.model.registerManageModel.CreatTables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description： 部门管理服务层
 * @Author: wbf
 * @Date: 2022/1/26 21:46
 */
@Service
public class DepartmentManageService {
    /**
     * 获取当前登录用户的用户信息
     * 并根据id生成表名
     */
    User loginUser = LoginService.getLoginUser();
    CreatTables tables = new CreatTables(loginUser.getUserId());

    //部门管理mapper
    @Autowired
    DepartmentManageMapper departmentManageMapper;
    /**
     * 4.1获取部门列表
     * @param param 参数类
     * @return 部门列表
     */
    public ResponseWrapper getDepartmentList(GetDepartmentListParam param){
        param.setTableName("02044f2fcd41_department");
//        param.setTableName(tables.getDepartmentTableName());
        param.setSearchKey(DataProcessing.CreatFuzzyQuery(param.getSearchKey()));
        param.setPage(DataProcessing.CalculatePage(param.getPage(), param.getPageSize()));
        List<Map<String, Object>> mapList = departmentManageMapper.getDepartmentList(param);
        List<GetDepartmentListResult> results = CommonTool.mapListToBeanList(mapList,GetDepartmentListResult.class);
        return ResponseWrapper.markSuccess(results);
    }

    /**
     * 4.2新增或修改部门
     * @param param 修改参数
     * @return 是否成功
     */
    public ResponseWrapper addOrUpdateDepartment(AddOrUpdateDepartmentParam param){
        param.setTableName("02044f2fcd41_department");
//        param.setTableName(tables.getDepartmentTableName());
        departmentManageMapper.addOrUpdateDepartment(param);
        return ResponseWrapper.markSuccess();
    }

    /**
     * 4.3检查部门名称是否重复
     * @param param 部门名称
     * @return 是否重复
     */
    public ResponseWrapper checkDepartmentName(CheckDepartmentNameParam param){
        param.setTableName("02044f2fcd41_department");
//        param.setTableName(tables.getDepartmentTableName());
        Integer count = departmentManageMapper.checkDepartmentName(param);
        boolean isExist = count > 0;
        return ResponseWrapper.markSuccess(new CheckDepartmentNameResult(isExist));
    }

    /**
     * 4.4获取部门信息
     * @param param 部门id
     * @return 部门信息
     */
    public ResponseWrapper getUserInfo(GetDepartmentInfoParam param){
        param.setTableName("02044f2fcd41_department");
//      param.setTableName(tables.getDepartmentTableName());
        Map<String, Object> departmentInfo = departmentManageMapper.getDepartmentInfo(param);
        List<Map<String,Object>> mapList = new ArrayList<>();
        mapList.add(departmentInfo);
        List<GetDepartmentListResult> results = CommonTool.mapListToBeanList(mapList,GetDepartmentListResult.class);
        return ResponseWrapper.markSuccess(results.get(0));
    }

    /**
     * 4.5删除部门
     * @param param 部门id
     * @return 删除结果
     */
    public ResponseWrapper delDepartment(DelDepartmentParam param){
        param.setTableName("02044f2fcd41_department");
//      param.setTableName(tables.getDepartmentTableName());
        departmentManageMapper.delDepartment(param);
        return ResponseWrapper.markSuccess();
    }
}
